package mrdemo003;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class SortReduce2 extends Reducer<LongWritable, Text, LongWritable, IntWritable>{
	
	//序号计数器（所有的数据都汇聚到这个reduce处理中）
	private IntWritable num = new IntWritable(1);
	
	//相同的值的数据的个数统计
	private int count = 0;
	
	 @Override
	protected void reduce(LongWritable k2, Iterable<Text> v2s,
			Reducer<LongWritable, Text, LongWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		 //输出： 数据  排序值
		 //上面的代码，实现的去重并且升序排序；
		 //如果我们要保留所有的数据，那就如下处理：
		 
		 //下面的代码，考虑数据值相等的情况下，做并列排名
		 for(Text v : v2s) {
			 context.write(k2, num);
			 count ++;
		 }
		 num = new IntWritable(num.get() + count);
		 count = 0;
	}
}
